package com.phaeton.beemeup.updatehandlers;

import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;

import org.andengine.engine.handler.IUpdateHandler;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;

import android.util.Log;

import com.phaeton.beemeup.GameManager;
import com.phaeton.beemeup.entity.Bee;
import com.phaeton.beemeup.entity.Flower;
import com.phaeton.beemeup.entity.IComb;
import com.phaeton.beemeup.entity.IInsect;
import com.phaeton.beemeup.entity.Wasp;
import com.phaeton.beemeup.entity.WaspComb;

public class FlowerHandler extends IHandler{
	private Map<UUID, Flower> mFlowers;
	
	public FlowerHandler(GameManager gameManager) {
		super(gameManager);
		mFlowers = gameManager.getFlowers();
	}

	public void init() {
		//Darstellung
		mGameManager.getLevel().registerUpdateHandler(new IUpdateHandler(){
			@Override
			public void onUpdate(float pSecondsElapsed) {
				for(Flower flower: mFlowers.values()){
					if(flower.getHoney() == 3 && flower.getMaterial() == 3) flower.stopAnimation(0);
					if(flower.getHoney() == 3 && flower.getMaterial() == 2) flower.stopAnimation(1);
					if(flower.getHoney() == 3 && flower.getMaterial() == 1) flower.stopAnimation(2);
					if(flower.getHoney() == 2 && flower.getMaterial() == 3) flower.stopAnimation(3);
					if(flower.getHoney() == 2 && flower.getMaterial() == 2) flower.stopAnimation(4);
					if(flower.getHoney() == 2 && flower.getMaterial() == 1) flower.stopAnimation(5);
					if(flower.getHoney() == 1 && flower.getMaterial() == 3) flower.stopAnimation(6);
					if(flower.getHoney() == 1 && flower.getMaterial() == 2) flower.stopAnimation(7);
					if(flower.getHoney() == 1 && flower.getMaterial() == 1) flower.stopAnimation(8);
					if(flower.getHoney() == 0 && flower.getMaterial() == 3) flower.stopAnimation(9);
					if(flower.getHoney() == 0 && flower.getMaterial() == 2) flower.stopAnimation(10);
					if(flower.getHoney() == 0 && flower.getMaterial() == 1) flower.stopAnimation(11);
					if(flower.getMaterial() == 0){
						flower.stopAnimation(12);
						flower.addState(Flower.DESTROYED);
						flower.setHoney(0);
					}
				}
			}
			
			@Override
			public void reset() {
			}
		});
		
		//Honey regeneration
		mGameManager.getLevel().registerUpdateHandler(new TimerHandler(10, true, new ITimerCallback(){
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				for(Flower flower: mFlowers.values()){
					if((flower.getState() & (Flower.OCCUPIED | Flower.DESTROYED)) == 0){
						if(flower.getHoney() < 3) flower.changeHoney(1);
//						if(flower.getMaterial() < 3) flower.changeMaterial(1);
					}
				}
			}        	
        }));
		//Material regeneration
		mGameManager.getLevel().registerUpdateHandler(new TimerHandler(15, true, new ITimerCallback(){
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				for(Flower flower: mFlowers.values()){
					if((flower.getState() & Flower.DESTROYED) != 0){
						if(flower.getMaterial() < 3) flower.changeMaterial(1);
						if(flower.getMaterial() == 3) flower.removeState(Flower.DESTROYED);
					}
				}
			}        	
        }));
		
		//Collecting
		mGameManager.getLevel().registerUpdateHandler(new IUpdateHandler() {
			public void onUpdate(float pSecondsElapsed) {
				for(final Flower flower: mFlowers.values()){
					for(final Entry<UUID, Bee> entry: mGameManager.getBees().entrySet()){
						if(flower.getOccupant() != null && flower.getOccupant().equals(entry.getValue())
								&& flower.getHoney() > 0 && entry.getValue().getHoney() < 3){
							
							entry.getValue().addState(IInsect.COLLECTING);
							
							mGameManager.getLevel().registerUpdateHandler(new TimerHandler(2, true, new ITimerCallback(){

								@Override
								public void onTimePassed(TimerHandler pTimerHandler) {
									if(flower.getHoney() > 0 && entry.getValue().getHoney() < 3
											&& flower.getOccupant() != null && flower.getOccupant().equals(entry.getValue())){
										
										flower.changeHoney(-1);
										entry.getValue().changeHoney(1);
									}else{
										mGameManager.getLevel().unregisterUpdateHandler(pTimerHandler);
										entry.getValue().removeState(IInsect.COLLECTING);
									}
								}
				            	
				            }));
						}
					}
					
					for(final Entry<UUID, Wasp> entry: mGameManager.getWasps().entrySet()){
						if(flower.getOccupant() != null && flower.getOccupant().equals(entry.getValue()) 
								&& flower.getMaterial() > 0 && entry.getValue().getMaterial() < 3 ){
							
							entry.getValue().addState(IInsect.COLLECTING);
							
							mGameManager.getLevel().registerUpdateHandler(new TimerHandler(2.5f, true, new ITimerCallback(){

								@Override
								public void onTimePassed(TimerHandler pTimerHandler) {
									Log.e("WaspState", "Wasp State " + entry.getValue().getState());
									Log.e("FlowerMat", "Flower Mat " + flower.getMaterial());
									
									if(flower.getMaterial() > 0 && entry.getValue().getMaterial() < 3
											/*&& flower.getOccupant() != null && flower.getOccupant().equals(entry.getValue())*/){
										
										flower.changeMaterial(-1);
										entry.getValue().changeMaterial(1);
									}else{
										mGameManager.getLevel().unregisterUpdateHandler(pTimerHandler);
										entry.getValue().removeState(IInsect.COLLECTING);
										Log.e("WaspState", "Wasp State after all " + entry.getValue().getState() + " with mat "+entry.getValue().getMaterial());
									}
									
								}
				            	
				            }));
						}
					}
				}
			}

			public void reset() {
			}
          });
	}

}
